package com.katze.boot.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.katze.boot.common.exception.BusinessException;
import com.katze.boot.common.utils.EncryptUtils;
import com.katze.boot.plugins.shiro.annotations.RestControllerMapping;
import com.katze.boot.plugins.shiro.annotations.RestRequestMapping;
import com.katze.boot.system.domain.User;
import com.katze.boot.system.domain.UserSetup;
import com.katze.boot.system.service.UserService;
import com.katze.boot.system.service.UserSetupService;
import com.katze.common.Message;
import com.katze.common.Pagination;
import com.katze.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestControllerMapping("user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private UserSetupService userSetupService;

    @RestRequestMapping("detail")
    public Message detail(String username) {
        return Message.ok().data(this.userService.findDetailByName(username));
    }

    @RestRequestMapping(permits = "user:view", skip = true)
    public Message userList(Pagination pagination, User user) {
        return toDataTable(userService.findUsers(user, pagination));
    }

    @RestRequestMapping(method = RequestMethod.POST, permits = "user:add", desc = "新增用户")
    public Message addUser(User user) {
        boolean exists = this.userService.exists(user.getUsername());
        if (exists) {
            return Message.error("用户名已存在");
        } else {
            this.userService.createUser(user);
            return Message.ok("用户创建成功,默认密码:".concat(User.DEFAULT_PASSWORD));
        }
    }

    @RestRequestMapping(method = RequestMethod.PUT, permits = "user:update", desc = "修改用户")
    public Message updateUser(User user) {
        this.userService.updateUser(user);
        return Message.ok("修改用户成功");
    }

    @RestRequestMapping(method = RequestMethod.DELETE, permits = "user:delete", desc = "删除用户")
    public Message deleteUsers(String... usernames) {
        this.userService.deleteUsers(usernames);
        return Message.ok("删除用户成功");
    }

    @RestRequestMapping(value = "profile", method = RequestMethod.PUT)
    public Message updateProfile(User user) {
        this.userService.updateProfile(user);
        return Message.ok("修改个人信息成功");
    }

    @RestRequestMapping(value = "avatar", method = RequestMethod.PUT)
    public Message updateAvatar(String username, String avatar) {
        this.userService.updateAvatar(username, avatar);
        return Message.ok("头像已更换");
    }

    @RestRequestMapping(value = "setup", method = RequestMethod.PUT)
    public Message updateUserSetup(UserSetup setup) {
        this.userSetupService.update(setup);
        return Message.ok("个性化配置应用成功");
    }

    @RestRequestMapping("check/username")
    public Message checkUserName(String username) {
        boolean exists = this.userService.exists(username);
        return Message.ok().data(!exists);
    }

    @RestRequestMapping(value = "check/password")
    public Message checkPassword(String username, String password) {
        boolean exists = userService.exists(username, EncryptUtils.MD5Encrypt(username, password));
        return Message.ok().data(exists);
    }

    @RestRequestMapping(value = "password", method = RequestMethod.PUT)
    public Message updatePassword(String username, String password) {
        userService.updatePassword(username, password);
        return Message.ok("修改密码成功");
    }

    @RestRequestMapping(value = "password/reset", method = RequestMethod.PUT, permits = "user:reset")
    public Message resetPassword(String usernames) {
        String[] usernameArr = usernames.split(StringPool.COMMA);
        this.userService.resetPassword(usernameArr);
        return Message.ok("重置用户密码成功");
    }

    @RestRequestMapping(value = "excel", method = RequestMethod.POST, permits = "user:export")
    public void export(Pagination pagination, @RequestBody User user, HttpServletResponse response) throws BusinessException {
        try {
            List<User> users = this.userService.findUsers(user, pagination).getRecords();
            super.downXlsx("用户信息表", users, response);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败", e);
        }
    }
}